﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace MinimalFileBrowser
{
    public class MainForm : Form
    {
        private TreeView treeView;
        private ListView listView;
        private RichTextBox xmlViewer;

        public MainForm()
        {
            Text = "文件浏览器";
            Size = new Size(800, 600);

            // 创建分割面板
            SplitContainer splitContainer = new SplitContainer
            {
                Dock = DockStyle.Fill,
                SplitterDistance = 250
            };
            Controls.Add(splitContainer);

            // 左侧树形视图
            treeView = new TreeView { Dock = DockStyle.Fill };
            treeView.AfterSelect += TreeView_AfterSelect;
            splitContainer.Panel1.Controls.Add(treeView);

            // 右侧列表视图
            listView = new ListView
            {
                Dock = DockStyle.Fill,
                View = View.Details,
                FullRowSelect = true
            };
            listView.Columns.Add("名称", 200);
            listView.Columns.Add("类型", 100);
            listView.Columns.Add("大小", 100);
            listView.Columns.Add("修改日期", 150);
            listView.DoubleClick += ListView_DoubleClick;
            splitContainer.Panel2.Controls.Add(listView);

            // XML查看器
            xmlViewer = new RichTextBox
            {
                Dock = DockStyle.Fill,
                ReadOnly = true,
                Visible = false
            };
            splitContainer.Panel2.Controls.Add(xmlViewer);

            // 加载用户目录
            LoadUserDirectory();
        }

        private void LoadUserDirectory()
        {
            try
            {
                string userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                TreeNode rootNode = new TreeNode(userDir);
                rootNode.Tag = userDir;
                treeView.Nodes.Add(rootNode);
                LoadSubdirectories(rootNode);
            }
            catch { }
        }

        private void LoadSubdirectories(TreeNode parentNode)
        {
            try
            {
                string path = parentNode.Tag.ToString();
                foreach (string dir in Directory.GetDirectories(path))
                {
                    TreeNode childNode = new TreeNode(Path.GetFileName(dir));
                    childNode.Tag = dir;
                    parentNode.Nodes.Add(childNode);
                    childNode.Nodes.Add(""); // 让节点可以展开
                }
            }
            catch { } // 忽略权限错误
        }

        private void TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                string path = e.Node.Tag.ToString();
                listView.Visible = true;
                xmlViewer.Visible = false;
                listView.Items.Clear();

                // 显示文件夹
                foreach (string dir in Directory.GetDirectories(path))
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(dir));
                    item.SubItems.Add("文件夹");
                    item.SubItems.Add("");
                    item.SubItems.Add(File.GetLastWriteTime(dir).ToString());
                    item.Tag = dir;
                    listView.Items.Add(item);
                }

                // 显示文件
                foreach (string file in Directory.GetFiles(path))
                {
                    FileInfo info = new FileInfo(file);
                    ListViewItem item = new ListViewItem(info.Name);
                    item.SubItems.Add(info.Extension);
                    item.SubItems.Add(GetSizeString(info.Length));
                    item.SubItems.Add(info.LastWriteTime.ToString());
                    item.Tag = file;
                    listView.Items.Add(item);
                }

                // 如果是第一次展开，加载子目录
                if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text == "")
                {
                    e.Node.Nodes.Clear();
                    LoadSubdirectories(e.Node);
                }
            }
            catch { } // 忽略权限错误
        }

        private string GetSizeString(long size)
        {
            if (size < 1024) return size + " B";
            if (size < 1024 * 1024) return (size / 1024) + " KB";
            return (size / (1024 * 1024)) + " MB";
        }

        private void ListView_DoubleClick(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count == 0) return;

            string path = listView.SelectedItems[0].Tag.ToString();

            if (Directory.Exists(path))
            {
                // 打开文件夹
                TreeNode[] nodes = treeView.Nodes.Find(path, true);
                if (nodes.Length > 0)
                    treeView.SelectedNode = nodes[0];
            }
            else if (Path.GetExtension(path).ToLower() == ".xml")
            {
                // 显示XML内容
                try
                {
                    listView.Visible = false;
                    xmlViewer.Visible = true;
                    xmlViewer.Text = File.ReadAllText(path);
                }
                catch { }
            }
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
    }
}